<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layui
      th:replace="pages/common/main :: layout(~{::title}, ~{::section}, ~{}, ~{::script})">
    <head>
        <title>菜单管理</title>
    </head>
    <body>
        <section>

            <div class="layui-fluid">

                <script type="text/html" id="toolBar">
                    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="create" layui:auth="/sys/admin/save">
                        <i class="layui-icon layui-icon-file-b">新建管理员</i>
                    </a>
                    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="deleteBatch"
                       layui:auth="/sys/admin/batch-delete">
                        <i class="layui-icon layui-icon-delete">批量删除</i>
                    </a>
                </script>

                <script type="text/html" id="tool">
                    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail"
                       layui:auth="/sys/admin/detail">查看</a>
                    <a class="layui-btn layui-btn-xs" lay-event="update" layui:auth="/sys/admin/save">编辑</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"
                       layui:auth="/sys/admin/delete">删除</a>
                    <a class="layui-btn layui-btn-info layui-btn-xs" lay-event="assign" layui:auth="/sys/admin/assign">设置权限</a>
                </script>

                <div class="layui-card">
                    <div class="layui-card-body">
                        <div th:insert="~{pages/modular/system/admin/_search::search}"></div>
                        <table id="admin-table" lay-filter="admin-table-filter"></table>
                    </div>
                </div>

            </div>

        </section>

        <script th:inline="javascript">

            layui.use(["grid", "form", "ax", "dtree", "admin"], function () {
                var grid = layui.grid,
                    admin = layui.admin,
                    form = layui.form,
                    $ax = layui.ax,
                    col = [
                        {type: "numbers"},
                        {type: "checkbox"},
                        {field: "id", title: "用户ID", sort: true, width: 180, align: "center", hide: true},
                        {field: "username", title: "账号"},
                        {field: "phone", title: "手机号", edit: true},
                        {
                            field: "status", title: "状态", templet: function (d) {
                                var options = /*[[${statusLabels}]]*/{}
                                return Kong.genLabel(d.status, options)
                            }
                        },
                        {
                            field: "type", title: "类型", templet: function (d) {
                                var options = /*[[${typeLabels}]]*/{}
                                return Kong.genLabel(d.type, options)
                            }
                        },
                        {field: "createTime", title: "创建时间"},
                        // {field: "updateTime", title: "更新时间"},
                        {width: 280, toolbar: "#tool", title: "操作"}
                    ],
                    options = {
                        tableId: "admin-table",
                        tableFilter: "admin-table-filter",
                        sort: "server",
                        table: {
                            url: Kong.ctxPath + "/sys/admin/list",
                            method: "post",
                            cols: [col],
                            defaultToolbar: ["filter", "print",
                                {
                                    "title": "导出Excel",
                                    "layEvent": "outputExcel",
                                    "icon": "layui-icon-export"
                                }
                            ]
                        },
                        editor: {
                            url: Kong.ctxPath + "/sys/admin/edit"
                        },
                        eventOptions: {
                            create: {
                                url: "/sys/admin/form"
                            },
                            deleteBatch: {
                                url: "/sys/admin/batch-delete"
                            },
                            update: {
                                url: "/sys/admin/form"
                            },
                            delete: {
                                url: "/sys/admin/delete"
                            },
                            outputExcel: {
                                url: "/sys/admin/excel-list"
                            }
                        }
                    },
                    events = {
                        assign: function (obj, config, that) {

                            admin.open({
                                type: 2,
                                title: "[" + obj.data.username + "]权限分配",
                                area: ["800px", "600px"],
                                content: Kong.ctxPath + "/sys/admin/assign?userId=" + obj.data.id,  // 注意这里有.html()
                                success: function (layero, dIndex) {
                                }
                            })
                        }
                    };

                grid.render(options, events);

                form.on("switch(switch-filter)", function (data) {
                    var id = data.elem.getAttribute("data-id")
                    var ajax = new $ax(Kong.ctxPath + "/sys/admin/edit", function (data) {
                        Kong.success("修改成功！")
                    })
                    ajax.setDataDataValue("active", data.elem.checked ? 1 : 0)
                    ajax.setDataConditionValue("id", id)
                    ajax.sendJson()
                });

            });

        </script>


    </body>
</html>
